package com.hs.pre.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.hs.pre.model.PreEntry;

public class PreParse {

	private List listDoc ;
	private Map lstMap;
	private String xpathDoc = "add/doc"; 
	private String xpathLst = "response/lst"; 
	private SAXReader reader = new SAXReader();
	public  PreParse(File file)
	{
		try {
			reader.setXMLFilter(new TrimXMLFilter());
			init(reader.read(file));
		} catch (DocumentException e) { 
			e.printStackTrace();
		}
	}
	
	public  PreParse(String document)
	{
		File f = new File("data.xml");
		document.getBytes();
		try {
			Document doc = reader.read(new ByteArrayInputStream(document.getBytes()));
			this.init(doc);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
 
	private void init(Document doc){
		
		this.setDoc(doc);
		//this.setLst(doc);
	}
	
	 
	private void setDoc(Document doc)
	{
		 List list = doc.selectNodes(xpathDoc);
         Iterator it = list.iterator();
         this.listDoc = new ArrayList();
         while (it.hasNext()) {   
             Element elt = (Element) it.next();   
             Iterator iterator = elt.elements().iterator();
             Map map = new HashMap();
             while(iterator.hasNext())
             {
           	  Element eltChi = (Element)iterator.next();
           	  String  name =  eltChi.attributeValue("name");
           	  String  value = eltChi.getText();
           	  map.put(name, value);
             }
             listDoc.add(map);
          }
	}
	
	 
	private void setLst(Document doc){
		 List list = doc.selectNodes(xpathLst);
         Iterator it = list.iterator();
          this.lstMap = new HashMap();
         while (it.hasNext()) {
             Element elt = (Element) it.next();   
             setRecursive(elt);
          } 
	}
	
	private void setRecursive(Element elt)
	{
		if(elt.elements().isEmpty())
        { 
     	    String  name =  elt.attributeValue("name");
      	  String  value = elt.getText();
      	  lstMap.put(name, value);
       	 
        }else
        {
            Iterator iterator = elt.elements().iterator();
            while(iterator.hasNext())
            {
          	  Element eltChi = (Element)iterator.next();
          	  this.setRecursive(eltChi);
            }
         }
	}
	
	public  PreParse(OutputStream out)
	{
		
	}
	
	
	public List getDoc()
	{
		return listDoc;
	}
	
	
	
	public Map getLst()
	{
		return lstMap;
	}
	public static void main(String[] ags){
		PreParse inst = new PreParse(new File("D:\\program\\work-2008917\\preSpring\\preSpring\\data\\part1.xml"));
		List list = inst.getDoc();
		for(int i = 0;i<list.size();i++){
			Map m = (Map)list.get(i);
			PreEntry entry = new PreEntry(m);
			System.out.println("方名:" + entry.getFangm());
		} 
	}
}
